#!/usr/bin/perl -w
use strict;

for my $dir ( @ARGV ) {
    opendir(DIR, $dir) || die $!;
    for my $file ( readdir(DIR) ) {
	next unless $file =~ /(\S+)\.table$/;
	my $stem = $1;
	warn("$stem\n");
	open(my $R => "| R --no-save ") || die $!;
	printf $R 'size <- read.table("%s/%s",header=T,sep="\t",row.names=1)'."\n",$dir,$file;
	printf $R 'pdf("%s/%s.pdf")'."\n",$dir,$stem;
	printf $R 'barplot(t(size),xlab="Read Length",ylab="Total # Reads",main="Region %s reads by size 5\'base",space=0.1,cex.axis=0.8,las=1,cex=0.8,names=size$V1,legend=T,col=rainbow(5,start=.1,end=.91),beside=F)'."\n",$stem;
    }

}
